package de.nordakademie.wpk.boerse.ui.provider;

import java.text.DecimalFormat;

import org.eclipse.jface.viewers.StyledCellLabelProvider;
import org.eclipse.jface.viewers.StyledString;
import org.eclipse.jface.viewers.TreeNode;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.swt.graphics.Image;
import de.nordakademie.wpk.boerse.core.data.Depot;
import de.nordakademie.wpk.boerse.core.data.INamedObject;
import de.nordakademie.wpk.boerse.core.data.Stock;
import de.nordakademie.wpk.boerse.utils.BoersePreferences;
import de.nordakademie.wpk.boerse.utils.BoerseTreeNode;

/**
 * Klasse stellt die Labels der einzelnen Knoten des Baumes zur Verfügung
 * 
 * @author Erkan Er
 *         http://eclipsesource.com/blogs/2009/03/10/tip-styling-label-providers
 *         /
 */
public class TreeLabelProvider extends StyledCellLabelProvider {

	public void update(ViewerCell cell) {

		int columnIndex = cell.getColumnIndex();
		Object element = cell.getElement();
		StyledString styledString = null;
		switch (columnIndex) {
		case 0:
	
			styledString = new StyledString(getColumnText(element, columnIndex));
			BoerseTreeNode node = (BoerseTreeNode) element;
			INamedObject value = (INamedObject) node.getValue();

			// showChildCount - Zähler anzeigen
			if (node.isShowChildrenCount()) {
				styledString.append(" (" + node.getChildren().length + ")",
						StyledString.COUNTER_STYLER);
			}

			// showDetails - Details anzeigen
			if (node.isShowDetails()) {
				DecimalFormat df = new DecimalFormat("0.000");

				if (value != null) {
					// Index
					if (value
							.getClass()
							.getName()
							.equals("de.nordakademie.wpk.boerse.core.data.Index")) {
					}
					// Depot
					else if (value
							.getClass()
							.getName()
							.equals("de.nordakademie.wpk.boerse.core.data.Depot")) {
						styledString.append(
								"  \u03A3: "
										+ df.format((double) ((Depot) value)
												.getValue().doubleValue())
										+ " EUR",
								StyledString.DECORATIONS_STYLER);
					}
					// Stock
					else if (value
							.getClass()
							.getName()
							.equals("de.nordakademie.wpk.boerse.core.data.Stock")) {
						styledString.append(
								"  Kurs: "
										+ df.format((double) ((Stock) value)
												.getLast().doubleValue())
										+ " EUR",
								StyledString.DECORATIONS_STYLER);
						Depot curDepot = ((Stock) value).getDepot();
						if (curDepot != null) {
							styledString
									.append("  "
											+ curDepot
													.getAmountOfStock((Stock) value)
											+ " St\u00fcck",
											StyledString.QUALIFIER_STYLER);
						}
					}
				}
			}

			cell.setText(styledString.toString());
			cell.setStyleRanges(styledString.getStyleRanges());
			cell.setImage(getColumnImage(element, columnIndex));
			super.update(cell);
			break;
		case 1:
			String description = getColumnText(element, columnIndex);
			if (description != null && description.length() > 0) {
				styledString = new StyledString(description,
						StyledString.QUALIFIER_STYLER);
				cell.setStyleRanges(styledString.getStyleRanges());
				cell.setText(styledString.toString());
			} else {
				cell.setText("");
			}
			cell.setImage(getColumnImage(element, columnIndex));
			super.update(cell);
		}

	}

	public Image getColumnImage(Object element, int columnIndex) {
		if (columnIndex == 0) {
			String className = (((TreeNode) element).getValue()).getClass()
					.getName();

			if (className.equals("de.nordakademie.wpk.boerse.core.data.Stock"))
				return BoersePreferences.STOCK_IMG;
			else if (className
					.equals("de.nordakademie.wpk.boerse.core.data.Index"))
				return BoersePreferences.INDEX_IMG;
			else if (className
					.equals("de.nordakademie.wpk.boerse.core.data.Depot"))
				return BoersePreferences.DEPOT_IMG;
			else
				return null;
		}
		return null;
	}

	public String getColumnText(Object element, int columnIndex) {
		switch (columnIndex) {
		case 0:
			String name = ((INamedObject) ((TreeNode) element).getValue())
					.getName();
			String result = name;
			return result;
		case 1:
			Object nodeValue = ((TreeNode) element).getValue();
			if (nodeValue.getClass().getName()
					.equals("de.nordakademie.wpk.boerse.core.data.Depot")
					|| nodeValue
							.getClass()
							.getName()
							.equals("de.nordakademie.wpk.boerse.core.data.Index"))
				return ((Depot) nodeValue).getDescription();
		}
		return "";
	}
}
